﻿@{
    var cate = ViewBag.cate as IEnumerable<bsuc.common.Model.Bsuc_Protal_Category>;
}
@Styles.Render("~/static/kindeditor/themes/default/default.css")
@Styles.Render("~/static/kindeditor/plugins/code/prettify.css")
@Scripts.Render("~/static/kindeditor/kindeditor-all.js")
@Scripts.Render("~/static/kindeditor/lang/zh-CN.js")
@Scripts.Render("~/static/kindeditor/plugins/code/prettify.js")
<script>
    var editor;
    KindEditor.ready(function (K) {
        editor = K.create('#UMeditor', {
            uploadJson: "'.$url.'",
            allowFileManager: false,
            minHeight: 300,
            width: "100%",
            afterBlur: function () { this.sync(); }
        });
    });
</script>
<form class="layui-form layui-form-pane" id="addposts" action="AddPost" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">文章标题</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="post_title"
                autocomplete="off" placeholder="请输入新闻标题">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">封面</label>
        <div class="layui-upload">
            <button class="layui-btn layui-btn-normal" id="test8" type="button">添加图片</button>
            <a id="uploadimg"></a>
            <a id="imgpath"></a>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文章类别</label>
        <div class="layui-input-inline">
            <select name="cates" class="field-role_id" type="select">
                <option value="" selected=""></option>
                @foreach (var item in cate)
                {
                    <option value="@Html.DisplayFor(val => item.id)">@Html.DisplayFor(val => item.catname)</option>
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新闻作者</label>
        <div class="layui-input-inline">
            <select name="user_id" class="field-role_id" type="select">
                <option value="@Session["user_id"]">@Session["nick_name"]</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">关键字</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input field-nick" name="post_ketwords" lay-verify="title"
                autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新闻摘要</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" id="" name="post_excerpt"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新闻内容</label>
        <div class="layui-input-block">
            <textarea id="UMeditor" name="post_content"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发布状态</label>
        <div class="layui-input-block">
            <input type="radio" class="field-status" name="post_status" value="1" title="发布" checked>
            <input type="radio" class="field-status" name="post_status" value="0" title="不发布">
        </div>
    </div>
    <br />
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="upload">立即提交</button>
            <button id="sub" lay-submit lay-filter="addArticle" style="display: none;"></button>
            <button class="layui-btn layui-btn-primary" type="reset">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','upload'], function () {
        var $ = layui.jquery, layer = layui.layer, form = layui.form
        , upload = layui.upload;
        form.on('submit(addArticle)', function (data) {
            delete data.field.fileData;
            var _form = $(this).parents('form');
            layer.msg('数据提交中...', { time: 500000 });
            $.ajax({
                type: "POST",
                url: _form.attr('action'),
                dataType: 'json',
                data: _form.serialize(),
                success: function (res) {
                    console.log(res);
                    layer.msg(res.msg, {}, function () {
                        if (res.code == 1) {
                            if (typeof (res.url) != 'undefined' && res.url != null && res.url != '') {
                                location.href = res.url;
                            } else {
                                location.reload();
                            }
                        } else {
                            location.reload();
                        }
                    });
                }, error: function (er) {
                    console.log(er);
                }
            });
            return false;
        });
        upload.render({
            elem: '#test8'
          , url: '/admin/upload/upload'
          , auto: false
          , field: "fileData"
          , bindAction: '#upload'
          , choose: function (obj) {
              //读取本地文件
              obj.preview(function (index, file, result) {
                  var img = '<img src="' + result + '" alt="' + file.name + '" style="width:65px;height:38px;margin:0 5px;" class="layui-upload-img">';
                  $("#uploadimg").append(img);
              });
          }
          , done: function (res) {
              console.log(res);
              $("#imgpath").append('<input name="cober" value=' + res.data.path + '>');
              document.getElementById("sub").click();
              return;
          }
        });
    });

</script>
